package com.tlsy.commerce.filter;

import com.tlsy.commerce.model.User;
import com.tlsy.commerce.multi_tenant.TenantHolder;
import com.tlsy.commerce.utils.AppConfig;
import com.tlsy.commerce.utils.ShiroUtil;
import org.springframework.web.filter.GenericFilterBean;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;

/**
 * 将多租户信息放入系统
 * @author Tlsy
 * @version commerce 0.0.1
 * @date 2017/4/20  17:01
 */
public class MultiTenantFilter extends GenericFilterBean {
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

        Object object = ShiroUtil.getSubject().getSession().getAttribute(AppConfig.USER_SESSION);
        if(object != null){
            User user = (User) object;
            TenantHolder.setTenant(user.getId());
        }
        chain.doFilter(request, response);
    }
}
